﻿using DJ.Dialog;
using NSJL.Biz.Background.Client;
using NSJL.DAL.DataModel.Entities;
using NSJL.DomainModel.Background.System;
using NSJL.Framework.Utils;
using NSJL.Plugin.Third.ThirdParty;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DJ
{
    /// <summary>
    /// ActivationView.xaml 的交互逻辑
    /// </summary>
    public partial class ActivationView : Window
    {
        CommBiz biz = new CommBiz();
        public ActivationView()
        {
            InitializeComponent();
            tb1.Focus();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (string.IsNullOrEmpty(tb.Text))
            {
                lb1.Visibility = Visibility.Visible;
            }
            else
            {
                lb1.Visibility = Visibility.Hidden;
            }
            if (tb.Text.Length > 19)
            {
                tb.Text = tb.Text.Substring(0, 19);
            }

            tb.SelectionStart = tb.Text.Length;
            tb.SelectionLength = 0;
            tb.Focus();
        }

        private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb.Text.Length == 4)
            {
                if (e.Key != Key.Back)
                {
                    tb.Text = tb.Text + "-";
                }
            }
            else if (tb.Text.Length == 9)
            {
                if (e.Key != Key.Back)
                {
                    tb.Text = tb.Text + "-";
                }
            }
            else if (tb.Text.Length == 14)
            {
                if (e.Key != Key.Back)
                {
                    tb.Text = tb.Text + "-";
                }
            }
        }

        //确定
        private void btn_OK(object sender, RoutedEventArgs e)
        {
            string code = tb1.Text;
            if (string.IsNullOrEmpty(code))
            {
                MessageDialog.ShowDialog("请输入你的序列号");
                return;
            }
            string cpu = SelfUtil.GetCPUSerialNumber();
            string str = "{\"physicalAddress\":\"" + cpu + "\",\"serialNumber\":\"" + code + "\"}";
            string v = RSAHelper.EncryptByPublicKey(str);
            AuthCommonResult result = InterfaceBiz.GetAuth(v);
            if (!result.success)
            {
                MessageDialog.ShowDialog(result.message);
                TextLogUtil.Info(result.message);
                return;
            }
            AuthenticationInfo authenticationInfo = new JavaScriptSerializer().Deserialize<AuthenticationInfo>(RSAHelper.DecryptByPublicKey(result.data.encryptContent));
            if(authenticationInfo != null)
            {
                biz.AddAuthInfo(authenticationInfo);
                this.DialogResult = true;
                return;
            }
            MessageDialog.ShowDialog("激活失败："+result.message);
            //this.DialogResult = false;
        }

        private void tb1_GotFocus(object sender, RoutedEventArgs e)
        {

           
        }
    }
}
